<?php

namespace App\Http\Controllers\Index;

use App\Facades\Site;
use App\Models\Admin\AdManage;
use App\Models\Admin\AreaStation;
use App\Models\Admin\Links;
use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use App\Models\Common\CollPosition;
use App\Models\Company\CompanyJob;
use App\Models\Personal\BrowseResume;
use App\Models\Personal\Interview;
use App\Models\Personal\ResumeSend;
use App\Models\News\NewsCategory;
use App\Models\News\News;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Style;

trait TemplateIndex{
    /**
     * 自定义模板控制器
     * @return string
     */
    protected function testIndex(){
        return '测试自定义模板控制器.';
    }

    /**
     * 自定义green模板控制器
     * @return string
     */
    protected function greenIndex(Request $request){
        //获取当前URL域名地址
        $url = getUrl();
        //默认主站的标识和编号
        $station_sign = 'main';
        $station_cid = '';
        $number = 9;//默认9
        $station = AreaStation::
        where(function($query) use ($url){
            if(!empty($url)){
                $query->orWhere('station_domain', '=', $url)
                    ->orWhere('station_domain', '=', $url . '/');
            }
        })
            ->first();
        if(!empty($station)){
            $station_sign = $station->station_sign;
            $station_cid = $station->station_cid;
            $number = 18;
        }
        //职位分类
        $position = CollPosition::getFirstPosition();
        $positionAllThree = CollPosition::getAllThree();
        //检查用户是不是登录
        $interview_count = 0;
        $browse_resume_count = 0;
        if(auth()->guard('personal')->check()){
            $user = auth()->guard('personal')->user();
            //面试邀请数量
            $interview_count = Interview::where('personal_uid', $user->uid)->where('status', '!=', 4)->count();
            //谁看过我数量
            $browse_resume_count = BrowseResume::where('personal_uid', $user->uid)->count();
        }elseif(auth()->guard('company')->check()){
            $company_user = auth()->guard('company')->user();
            //查收简历
            $resumeSendCount = ResumeSend::where('company_uid', $company_user->uid)->where('status', '!=', 4)->count();
            $companyJobCount = CompanyJob::where('uid', $company_user->uid)->where('job_status', '!=', 4)->count();
        }

        //获取职位列表(今日高薪)
        $jobList = DB::table('company_job as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->join('company_users as t3', 't1.uid', '=', 't3.uid')
            ->select('t1.id', 't1.com_id', 't1.salary_above', 't1.urgent', 't1.salary_below', 't1.job_title', 't2.company_name')
            //工作地点(某个地区下)
            ->where(function($query) use ($station_cid){
                if(!empty($station_cid)){
                    $query->orWhere('t1.location1', '=', $station_cid)
                        ->orWhere('t1.location2', '=', $station_cid)
                        ->orWhere('t1.location3', '=', $station_cid)
                        ->orWhere('t1.location4', '=', $station_cid)
                        ->orWhere('t1.location5', '=', $station_cid);
                }
            })
            ->where('t1.job_status', '1')
            ->where('t3.viptime', '>=', date('Y-m-d H:i:s', time()))
            ->where('salary_above', '>=', '5000')
            ->orderBy('t1.updatetime', 'desc')->take(6)->get();
        $jobList = $this->jobInfo($jobList);

        /*@param 获取广告信息
         * 第一参数 广告位站点标识
         * 第二参数 广告位位置标识
         */
        //登录框banner广告
        $loginT1 = AdManage::getAd($station_sign, 'T1');

        //登录框显示职位广告
        $loginT2 = AdManage::getAd($station_sign, 'T2');
        if(!empty($loginT2)){
            foreach($loginT2 as $v){
                if(!empty($v->com_id)){
                    //获取加急职位
                    $companyJob = CompanyJob::where('com_id', $v->com_id)->where('job_status', '1')
                        ->orderBy('urgent', 'desc')->select('job_title')->get();
                    if(count($companyJob) > 0){
                        $job_title = '';
                        foreach($companyJob as $vv){
                            $job_title .= $vv->job_title . '、';
                        }
                        $v->job_title = trim($job_title, '、');
                    }else{
                        $v->job_title = '';
                    }
                }else{
                    $v->job_title = '';
                }
            }
        }
        //登录框下方广告
        $loginT3 = AdManage::getAd($station_sign, 'T3');
        //首页名企 默认为台州站
        $mingP = AdManage::getAd($station_sign, 'M1');
        //首页中部半长条
        $half = AdManage::getAd($station_sign, 'P1');
        //首页中部短条
        $short = AdManage::getAd($station_sign, 'P2');
        //友情链接获取
        $links = Links::getLinks($station_sign);
        //默认强烈推荐急招 显示不限
        $recommend = DB::table('company_job as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->select('t1.id', 't1.com_id', 't1.salary_above', 't1.salary_below', 't1.job_title', 't1.location1', 't1.updatetime', 't1.urgent', 't2.company_name')
            //工作地点(某个地区下)
            ->where(function($query) use ($station_cid){
                if(!empty($station_cid)){
                    $query->orWhere('t1.location1', '=', $station_cid)
                        ->orWhere('t1.location2', '=', $station_cid)
                        ->orWhere('t1.location3', '=', $station_cid)
                        ->orWhere('t1.location4', '=', $station_cid)
                        ->orWhere('t1.location5', '=', $station_cid);
                }
            })
            //职位显示审核通过的
            ->where('t1.job_status', '1')
            ->orderBy('t1.urgent', 'desc')
            ->orderBy('t1.updatetime', 'desc')
            ->take($number)
            ->get();
        $recommend = $this->jobInfo($recommend);

        //分类类别查询
        //销售/营销/贸易/外贸 职位急招
        $sellCateArr = ['11', '24'];
        $sellCate = $this->cateSelect($sellCateArr, $station_cid);
        //文职/行政/客服类
        $civilianCateArr = ['12'];
        $civilianCate = $this->cateSelect($civilianCateArr, $station_cid);
        //餐饮/百货/生活服务
        $foodCateArr = ['20'];
        $foodCate = $this->cateSelect($foodCateArr, $station_cid);
        //广告/设计/传媒
        $advertisingCateArr = ['19'];
        $advertisingCate = $this->cateSelect($advertisingCateArr, $station_cid);
        //能源/化工/服装
        $energyCateArr = ['23'];
        $energyCate = $this->cateSelect($energyCateArr, $station_cid);

        //By 小周 讯息分类
        $newsImgExist = DB::table('news_img_exist as t1')
            ->join('news as t2', 't1.news_id', '=', 't2.id')
            ->select('t2.title', 't2.editorValue', 't2.readnum', 't2.updated_at', 't2.id')
            ->where('t1.img_state', 1)
            ->groupBy('t1.news_id')
            ->orderBy('t2.updated_at', 'desc')
            ->take(2)
            ->get();
        foreach($newsImgExist as $k => $v){
            //有图片显示，无显示文本
            if(preg_match_all('/<img(.*)?\s*src=\"(.*)\"(.*)\/>/Usi', $v->editorValue, $arr)){
                $v->imgUrl = $arr[2][0];//取出第一张
                $v->content = strip_tags($v->editorValue);
            }else{
                $v->content = strip_tags($v->editorValue);
            }
            $v->updated_at = substr($v->updated_at, 0, 16);
        }
        //去除存在的资讯
        $id = array_pluck($newsImgExist, 'id');
        //获取更多的资讯
        $moreNews = News::whereNotIn('id', $id)->select('title','id')->orderBy('updated_at', 'desc')->take(10)->get();

        if(!empty($station_sign) && $station_sign != 'main'){
//            return Style::view('index.stationIndex', compact('position', 'positionAllThree', 'jobList', 'interview_count', 'browse_resume_count',
//                'resumeSendCount', 'companyJobCount',
//                'loginT1', 'loginT2', 'loginT3', 'mingP', 'half', 'short', 'links', 'recommend',
//                'sellCate', 'civilianCate', 'foodCate', 'advertisingCate', 'energyCate',
//                'secondClass', 'siteNotice', 'resumeAndInterview', 'institution', 'station_cid', 'station'));
        }else{
            return view('green.index.index', compact('position', 'positionAllThree', 'jobList', 'interview_count', 'browse_resume_count',
                'resumeSendCount', 'companyJobCount',
                'loginT1', 'loginT2', 'loginT3', 'mingP', 'half', 'short', 'links', 'recommend',
                'sellCate', 'civilianCate', 'foodCate', 'advertisingCate', 'energyCate','newsImgExist',
                'moreNews'));
        }
    }

    /**
     * 职位信息处理
     * @param $company_job
     * @return array
     */
    public function jobInfo($company_job){
        if(count($company_job) > 0){
            foreach($company_job as $v){
                //急聘
                if($v->urgent > date('Y-m-d H:i:s', strtotime('-5 day'))){
                    $v->urgent = 1;
                }else{
                    $v->urgent = 0;
                }
                $v->joburl = 'jobinfo_' . idEncryption($v->id) . '.html';
                $v->comurl = 'comabout_' . idEncryption($v->com_id) . '.html';
                $v->salary_above = !empty($v->salary_above) ? CollOption::getOptionName($v->salary_above, 'expectedsalary') : '';
                $v->salary_below = !empty($v->salary_below) ? CollOption::getOptionName($v->salary_below, 'expectedsalary') : '';
                $v->money = ($v->salary_above) ? $v->salary_above . '~' . $v->salary_below . '元/月' : '面议';
                $v->location1 = !empty($v->location1) ? CollArea::getAreaName($v->location1) : '';
            }
            return $company_job;
        }else{
            return array();
        }
    }

    /**
     * 分类信息查询
     * @param $cateArr
     * @return array
     */
    public function cateSelect($cateArr, $station_cid = ''){
        $cate = DB::table('company_job as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->select('t1.id', 't1.com_id', 't1.salary_above', 't1.salary_below', 't1.job_title', 't1.urgent', 't1.location1', 't1.updatetime', 't2.company_name')
            ->where(function($query) use ($cateArr){
                foreach($cateArr as $v){
                    $query->orWhere('t1.job_category1', 'like', $v . '%')
                        ->orWhere('t1.job_category2', 'like', $v . '%')
                        ->orWhere('t1.job_category3', 'like', $v . '%')
                        ->orWhere('t1.job_category4', 'like', $v . '%')
                        ->orWhere('t1.job_category5', 'like', $v . '%');
                }
            })
            ->where(function($query) use ($station_cid){
                if(!empty($station_cid)){
                    $query->orWhere('t1.location1', '=', $station_cid)
                        ->orWhere('t1.location2', '=', $station_cid)
                        ->orWhere('t1.location3', '=', $station_cid)
                        ->orWhere('t1.location4', '=', $station_cid)
                        ->orWhere('t1.location5', '=', $station_cid);
                }
            })
            //职位显示审核通过的
            ->where('t1.job_status', '1')
            ->orderBy('t1.urgent', 'desc')
            ->orderBy('t1.updatetime', 'desc')
            ->take(12)->get();
        if(count($cate) > 0){
            foreach($cate as $v){
                //急聘
                if($v->urgent > date('Y-m-d H:i:s', strtotime('-5 day'))){
                    $v->urgent = 1;
                }else{
                    $v->urgent = 0;
                }
                $v->joburl = 'jobinfo_' . idEncryption($v->id) . '.html';
                $v->comurl = 'comabout_' . idEncryption($v->com_id) . '.html';
                $v->job_title = str_limit($v->job_title, 20);
                $v->company_name = str_limit($v->company_name, 16);
                $v->salary_above = !empty($v->salary_above) ? CollOption::getOptionName($v->salary_above, 'expectedsalary') : '';
                $v->salary_below = !empty($v->salary_below) ? CollOption::getOptionName($v->salary_below, 'expectedsalary') : '';
                $v->money = ($v->salary_above) ? $v->salary_above . '~' . $v->salary_below . '元/月' : '面议';
                $v->location1 = !empty($v->location1) ? CollArea::getAreaName($v->location1) : '';
            }
            return $cate;
        }else{
            return array();
        }
    }
}